﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Xml;
namespace Zebra.Umbraco.Marcos.Code.QuestionType
{
    [Serializable]
    [XmlInclude(typeof(RegularQuestion))]
    public abstract class Question
    {

        public int QuestionID
        {
            get;
            set;
        }

        public int SurveyID
        {
            get;
            set;
        }

        public int PageID
        {
            get;
            set;
        }

        public string QuestionText
        {
            get;
            set;
        }

        public bool MustAnswer
        {
            get;
            set;
        }

        public string ToXml()
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter xw = XmlWriter.Create(sb);
            XmlSerializer serializer = new XmlSerializer(this.GetType());
            serializer.Serialize(xw, this);
            xw.Close();
            return sb.ToString();

        }

        public static Question FromXML(string Type, string XML)
        {
            XmlSerializer serializer;
            StringReader sr;
            switch (Type)
            {
                case "RegularQuestion":
                    serializer = new XmlSerializer(typeof(RegularQuestion));
                    sr = new StringReader(XML);
                    return (RegularQuestion)serializer.Deserialize(sr);
                case "CommentQuestion":
                    serializer = new XmlSerializer(typeof(CommentQuestion));
                    sr = new StringReader(XML);
                    return (CommentQuestion)serializer.Deserialize(sr);

            }
            return null;

        }

        #region abstract methods
        protected abstract string GenerateBodyHTML();
        public abstract QuestionCatalog Catalog { get; }
        #endregion

        public virtual string GenerateHTML(bool IsEditing)
        {
            string retVal = "";
            retVal += BuildTitle();
            retVal += GenerateBodyHTML();
            retVal += BuildEnd(IsEditing);
            return retVal;

        }

        #region HTML helper
        protected string BuildRadioButton(string Answer, int Index)
        {
            return string.Format("<input type=\"radio\" value=\"{0}\" name=\"{1}\" id=\"{2}\" /><label for=\"{2}\">{3}</label> <br />", Index, string.Format("Answer_", QuestionID), string.Format("Answer{0}_{1}", QuestionID, Index), Answer);
        }

        protected string BuildCheckBox(string Answer, int Index)
        {
            return string.Format("<input type=\"checkbox\" value=\"{0}\" name=\"{1}\" id=\"{2}\" /><label for=\"{2}\">{3}</label> <br />", Index, string.Format("Answer_", QuestionID), string.Format("Answer{0}_{1}", QuestionID, Index), Answer);
        }

        protected virtual string BuildTitle()
        {
            return string.Format("<div id='QuestionDIV_{0}_{1}'><h4>{2}</h4>", SurveyID, QuestionID, QuestionText);
        }

        protected virtual string BuildEnd(bool IsEditing)
        {
            if (IsEditing)
            {
                return string.Format("<p class='no-border'><input type='button' class='button' onclick=\"EditQuestion('{2}','{1}','QuestionDIV_{0}_{1}')\" value='修改问题'></p></div>", SurveyID, QuestionID, (int)Catalog);
            }
            else
            {
                return "</div>";
            }
        }
        #endregion

    }

    public enum QuestionCatalog
    {
        RegularQuestion = 1,
        CommentQuestion = 2
    }
}